package com.itheima.dubbo.mongodb.impl;


import cn.hutool.core.collection.CollUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.commons.model.mongo.UserLike;
import com.itheima.dubbo.interfaces.UserLikeApi;
import com.mongodb.client.result.DeleteResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service
public class UserLikeApiApiImpl implements UserLikeApi {

    @Autowired
    private MongoTemplate mongoTemplate;


    // 喜欢
    @Override
    public void likeUser(UserLike userLike) {
        mongoTemplate.save(userLike);
    }

    // 取消喜欢
    @Override
    public Boolean notLikeUser(Long userId, Long likeUserId) {
        DeleteResult result = mongoTemplate.remove(Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId)), UserLike.class);
        return result.getDeletedCount() > 0;
    }

    // 是否喜欢
    @Override
    public Boolean isLike(Long userId, Long likeUserId) {
        UserLike one = mongoTemplate.findOne(Query.query(Criteria.where("userId").is(userId)
                .and("likeUserId").is(likeUserId)), UserLike.class);
        return one != null;
    }


    // 判断是否互相喜欢
    @Override
    public Boolean isMutualLike(Long userId, Long likeUserId) {
        return isLike(userId, likeUserId) && isLike(likeUserId, userId);
    }


    // 查询互相喜欢数
    @Override
    public Long queryMutualLikeCount(Long userId) {
        // 先查询我喜欢的人的ID
        List<UserLike> userLikes = mongoTemplate.find(Query.query(Criteria.where("userId").is(userId)), UserLike.class);
        List<Long> likeUserIds = CollUtil.getFieldValues(userLikes, "likeUserId", Long.class);
        long mutualCount = mongoTemplate.count(Query.query(Criteria.where("likeUserId").is(userId)
                .and("userId").in(likeUserIds)), UserLike.class);
        return mutualCount;
    }

    // 查询我喜欢的人的数量
    @Override
    public Long queryLikeCount(Long userId) {
        return mongoTemplate.count(Query.query(Criteria.where("userId").is(userId)), UserLike.class);
    }

    // 查询粉丝数量
    @Override
    public Long queryFanCount(Long userId) {
        return mongoTemplate.count(Query.query(Criteria.where("likeUserId").is(userId)), UserLike.class);
    }

    // 分页查询互相喜欢的列表
    @Override
    public List<UserLike> queryMutualLikeList(Long userId, Integer page, Integer pageSize) {
        List<UserLike> userLikes = mongoTemplate.find(Query.query(Criteria.where("userId").is(userId)), UserLike.class);
        List<Long> likeUserIds = CollUtil.getFieldValues(userLikes, "likeUserId", Long.class);
        return mongoTemplate.find(Query.query(Criteria.where("likeUserId").is(userId)
                        .and("userId").in(likeUserIds))
                        .with(PageRequest.of(page - 1, pageSize))
                        .with(Sort.by(Sort.Order.desc("created")))
                , UserLike.class);
    }

    // 分页查询喜欢列表
    @Override
    public List<UserLike> queryLikeList(Long userId, Integer page, Integer pageSize) {
        return mongoTemplate.find(Query.query(Criteria.where("userId").is(userId))
                        .with(PageRequest.of(page - 1, pageSize))
                        .with(Sort.by(Sort.Order.desc("created")))
                , UserLike.class);
    }

    // 分页查询粉丝列表
    @Override
    public List<UserLike> queryFanList(Long userId, Integer page, Integer pageSize) {
        return mongoTemplate.find(Query.query(Criteria.where("likeUserId").is(userId))
                        .with(PageRequest.of(page - 1, pageSize))
                        .with(Sort.by(Sort.Order.desc("created")))
                , UserLike.class);
    }
}
